import gap.maven.script.components.api.POMArtifactFile;
import gap.maven.script.components.api.ArtifactFileFactory
import gap.maven.script.components.api.POMArtifactFile
import gap.maven.script.components.api.ScriptContext

import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log

final Log log = binding.getVariable("log");
final ScriptContext context = binding.getVariable("context");
final ArtifactFileFactory factory = context.lookup(ArtifactFileFactory.SCRIPT_CONTEXT_KEY);

POMArtifactFile file = factory.createPOMFile("org.apache.maven", "maven-artifact", "2.2.1");
if(file.download()) {
    List<POMArtifactFile> deps = file.getDependencyPOMs();
    List<POMArtifactFile> managedDeps = file.getManagedDependencyPOMs();

    println("Number of deps : " + deps.size());
    println("Number of managed deps : " + managedDeps.size());

    println("Dependecy List -----------------------------")
    for(POMArtifactFile dep : deps) {
        if(dep.download()) {
            println(dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion());
        }
    }

    println("Managed Dependecy List ---------------------")
    for(POMArtifactFile dep : managedDeps) {
        if(dep.download()) {
            println(dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion());
        }
    }

    println("Version of org.codehaus.plexus:plexus-utils is " +
            file.findVersionInDependencies("org.codehaus.plexus", "plexus-utils"));

    println("Version of org.apache.maven.wagon:wagon-http is " +
            file.findVersionInManagedDependencies("org.apache.maven.wagon", "wagon-http"));

    try {
        println("Version of org.apache.maven.wagon:wagon-http is " +
                file.findVersionInDependencies("org.apache.maven.wagon", "wagon-http"));
    }catch (MojoExecutionException ex) {
        println(ex.getMessage());
    }

    try {
        println("Version of org.apache.maven.wagon:wagon-http1 is " +
                file.findVersionInManagedDependencies("org.apache.maven.wagon", "wagon-httpi"));
    }catch (MojoExecutionException ex) {
        println(ex.getMessage());
    }
}

POMArtifactFile depTestFile = factory.createPOMFile("org.apache.maven", "maven-model", "3.0.2");
if(depTestFile.download())
{
    List<Dependency> mvnDeps = file.getDependencies();
    mvnDeps.each ({
        println("Maven Dependency " + it.toString());
    });

    List<Dependency> mvnMDeps = file.getManagedDependencies();
    mvnMDeps.each ({
        println("Maven Managed Dependency " + it.toString());
    });

}